#include "UsrAI.h"
#include<set>
#include <iostream>
#include<unordered_map>
using namespace std;
tagGame tagUsrGame;
ins UsrIns;
tagInfo info;
int builddock=0;
int beginx = 0;
int beginy = 0;
int begin1x = 0;
int begin1y = 0;
int begin2x = 0;
int begin2y = 0;
int begin3x = 0;
int begin3y = 0;
int begin4x = 0;
int begin4y = 0;
int begin5x =0;
int begin5y=0;
int begin6x =0;
int begin6y=0;
int begin7x =0;
int begin7y=0;
int armycenterx=0;
int armycentery=0;
int chufa=0;
int tingkaox=0;
int tingkaoy=0;
int denglux=0;
int dengluy=0;
int buildboat=0;
int yidongduian=0;
int zhanchuanhuijia=0;
static int stage=1;
static int center_x=0;
static int center_y=0;
static int homePositions[4][2] = {0}; // 存储4个房屋的块坐标
static int homesnumber = 0; // 已建造的房屋数量
bool home_flag = false;
//
//
double Distance(double x1, double y1, double x2, double y2) {           //计算资源距离
    double dx = x1 - x2;
    double dy = y1 - y2;
    return sqrt(dx*dx + dy*dy);
}
int findResource(const tagInfo& info, int targetType, double workerDR, double workerUR) {   //找寻该资源最近位置
    int bestSN = -1;
        double min =10000;

        for (tagResource resource:info.resources) {
            if (resource.Type != targetType) {
                continue;
            }
            int beoccupied = 0;        //检查有没有村民在进行占用
            for (tagFarmer farmer:info.farmers) {
                if (farmer.WorkObjectSN == resource.SN && farmer.NowState == HUMAN_STATE_WORKING) {
                    beoccupied = 1;
                    break;
                }
            }
            if (beoccupied) {
                continue;
            }
            double distance = Distance(workerDR, workerUR, resource.DR, resource.UR);
            if (distance < min) {
                min = distance;
                bestSN = resource.SN;
            }
        }
        return bestSN;
}
int findResource_yiqi(const tagInfo& info, int targetType, double workerDR, double workerUR) {   //找寻该资源最近位置
    int bestSN = -1;
        double min =10000;

        for (tagResource resource:info.resources) {
            if (resource.Type != targetType) {
                continue;
            }
            double distance = Distance(workerDR, workerUR, resource.DR, resource.UR);
            if (distance < min) {
                min = distance;
                bestSN = resource.SN;
            }
        }
        return bestSN;
}
char themap[128][128];
char THEMAP[128][128];
int find_block(int beginx1,int beginy1,int& x, int& y)
{
    for (int i = beginx1; i < 128; i++) {
        for (int j = beginy1; j < 128; j++) {
            if (i>0&&j>0&&i + 1 < 128 && j + 1 < 128) {
                // 检查四个位置是否都是陆地
                if ((*info.theMap)[i][j].type == MAPPATTERN_GRASS &&
                    (*info.theMap)[i+1][j].type == MAPPATTERN_GRASS &&
                    (*info.theMap)[i][j + 1].type == MAPPATTERN_GRASS &&
                    (*info.theMap)[i+1][j + 1].type == MAPPATTERN_GRASS) {
                    // 检查高度是否相等
                    int height = (*info.theMap)[i][j].height;
                    if ((*info.theMap)[i+1][j].height == height &&
                        (*info.theMap)[i][j + 1].height == height &&
                        (*info.theMap)[i+1][j + 1].height == height) {
                        if(themap[i][j]!='*'&&themap[i+1][j]!='*'&&themap[i][j+1]!='*'&&themap[i+1][j+1]!='*'&&themap[i][j]!='H'&&themap[i+1][j]!='H'&&themap[i][j+1]!='H'&&themap[i+1][j+1]!='H'){
                        // 找到符合条件的2*2空地，返回左下角坐标
                        x = i;
                        y = j;

                        return 1; // 返回1表示成功找到
}
                    }
                }
            }
        }
    }
    return 0; // 返回0表示未找到合适的空地
}
int find_block_33(int beginx1,int beginy1,int& x, int& y)
{
    for (int i = beginx1; i < 128; i++) {
        for (int j = beginy1; j < 128; j++) {
            if (i + 2 < 128 && j + 2 < 128) {
                // 检查四个位置是否都是陆地
                if ((*info.theMap)[i][j].type == MAPPATTERN_GRASS &&
                    (*info.theMap)[i+1][j].type == MAPPATTERN_GRASS &&
                    (*info.theMap)[i+2][j].type == MAPPATTERN_GRASS &&
                    (*info.theMap)[i][j+1].type == MAPPATTERN_GRASS &&
                    (*info.theMap)[i+1][j+1].type == MAPPATTERN_GRASS &&
                    (*info.theMap)[i+2][j+1].type == MAPPATTERN_GRASS &&
                    (*info.theMap)[i][j+2].type == MAPPATTERN_GRASS &&
                    (*info.theMap)[i+1][j + 2].type == MAPPATTERN_GRASS&&
                    (*info.theMap)[i+2][j + 2].type == MAPPATTERN_GRASS){
                    // 检查高度是否相等
                    int height = (*info.theMap)[i][j].height;
                    if ((*info.theMap)[i][j+1].height == height &&
                        (*info.theMap)[i][j+2].height == height &&
                        (*info.theMap)[i+1][j].height == height &&
                        (*info.theMap)[i+1][j+1].height == height &&
                        (*info.theMap)[i+1][j+2].height == height &&
                        (*info.theMap)[i+2][j].height == height&&
                        (*info.theMap)[i+2][j+1].height == height &&
                        (*info.theMap)[i+2][j+2].height == height){
                        if(themap[i][j]!='*'&&themap[i+1][j]!='*'&&themap[i][j+1]!='*'&&themap[i+1][j+1]!='*'&&
                           themap[i+2][j]!='*'&&themap[i+2][j+1]!='*'&&themap[i+2][j+2]!='*'&&themap[i][j+2]!='*'&&themap[i+1][j+2]!='*'&&
                           themap[i][j]!='H'&&themap[i+1][j]!='H'&&themap[i][j+1]!='H'&&themap[i+1][j+1]!='H'&&
                           themap[i+2][j]!='H'&&themap[i+2][j+1]!='H'&&themap[i+2][j+2]!='H'&&themap[i][j+2]!='H'&&themap[i+1][j+2]!='H'){
                        // 找到符合条件的2*2空地，返回左下角坐标
                        x = i;
                        y = j;

                        return 1; // 返回1表示成功找到
}
                    }
                }
            }
        }
    }
    return 0; // 返回0表示未找到合适的空地
}
int find_block_33farm(int beginx1,int beginy1,int& x, int& y)
{
    for (int i = beginx1; i < 128; i=i+2) {
        for (int j = beginy1+2; j < 128; j=j+2) {
            if (i + 2 < 128 && j + 2 < 128) {
                // 检查四个位置是否都是陆地
                if ((*info.theMap)[i][j].type == MAPPATTERN_GRASS &&
                    (*info.theMap)[i+1][j].type == MAPPATTERN_GRASS &&
                    (*info.theMap)[i+2][j].type == MAPPATTERN_GRASS &&
                    (*info.theMap)[i][j+1].type == MAPPATTERN_GRASS &&
                    (*info.theMap)[i+1][j+1].type == MAPPATTERN_GRASS &&
                    (*info.theMap)[i+2][j+1].type == MAPPATTERN_GRASS &&
                    (*info.theMap)[i][j+2].type == MAPPATTERN_GRASS &&
                    (*info.theMap)[i+1][j + 2].type == MAPPATTERN_GRASS&&
                    (*info.theMap)[i+2][j + 2].type == MAPPATTERN_GRASS){
                    // 检查高度是否相等
                    int height = (*info.theMap)[i][j].height;
                    if ((*info.theMap)[i][j+1].height == height &&
                        (*info.theMap)[i][j+2].height == height &&
                        (*info.theMap)[i+1][j].height == height &&
                        (*info.theMap)[i+1][j+1].height == height &&
                        (*info.theMap)[i+1][j+2].height == height &&
                        (*info.theMap)[i+2][j].height == height&&
                        (*info.theMap)[i+2][j+1].height == height &&
                        (*info.theMap)[i+2][j+2].height == height){
                        // 找到符合条件的2*2空地，返回左下角坐标
                        x = i;
                        y = j;

                        return 1; // 返回1表示成功找到

                    }
                }
            }
        }
    }
    return 0; // 返回0表示未找到合适的空地
}
int find_block_dock(int& x, int& y)
{
    for (int i=0; i < 128; i++) {
        for (int j=0; j < 128; j++) {
            if (i+1 < 128 && j + 1 < 128) {
                // 检查四个位置是否都是陆地
                if ((*info.theMap)[i][j].type == MAPPATTERN_OCEAN &&
                    (*info.theMap)[i+1][j].type == MAPPATTERN_OCEAN &&
                    (*info.theMap)[i][j+1].type == MAPPATTERN_OCEAN &&
                    (*info.theMap)[i+1][j+1].type == MAPPATTERN_OCEAN){

                    // 检查高度是否相等
                    if (((*info.theMap)[i+2][j].type == MAPPATTERN_GRASS &&(*info.theMap)[i+2][j+1].type == MAPPATTERN_GRASS) ||
                        ((*info.theMap)[i-1][j].type == MAPPATTERN_GRASS &&(*info.theMap)[i-1][j+1].type == MAPPATTERN_GRASS) ||
                        ((*info.theMap)[i][j-1].type == MAPPATTERN_GRASS &&(*info.theMap)[i+1][j-1].type == MAPPATTERN_GRASS) ||
                        ((*info.theMap)[i][j+2].type == MAPPATTERN_GRASS && (*info.theMap)[i+1][j+2].type == MAPPATTERN_GRASS ))
                    {
                        // 找到符合条件的2*2空地，返回左下角坐标
                        x = i;
                        y = j;
                          cout<<x<<" "<<y<<endl;
                       return 1; // 返回1表示成功找到

                    }
                }
            }
        }
    }
    return 0; // 返回0表示未找到合适的空地
}
int find_block_dock2(int& x, int& y)
{
    for (int i=0; i < 128; i++) {
        for (int j=0; j < 128; j++) {
            if (i+1 < 128 && j + 1 < 128) {
                // 检查四个位置是否都是陆地
                if ((*info.theMap)[i][j].type == MAPPATTERN_OCEAN &&
                    (*info.theMap)[i+1][j].type == MAPPATTERN_OCEAN &&
                    (*info.theMap)[i][j+1].type == MAPPATTERN_OCEAN &&
                    (*info.theMap)[i+1][j+1].type == MAPPATTERN_OCEAN){

                    // 检查高度是否相等
                    if (((*info.theMap)[i+2][j].type == MAPPATTERN_GRASS &&(*info.theMap)[i+2][j+1].type == MAPPATTERN_GRASS) ||
                        ((*info.theMap)[i-1][j].type == MAPPATTERN_GRASS &&(*info.theMap)[i-1][j+1].type == MAPPATTERN_GRASS) ||
                        ((*info.theMap)[i][j-1].type == MAPPATTERN_GRASS &&(*info.theMap)[i+1][j-1].type == MAPPATTERN_GRASS) ||
                        ((*info.theMap)[i][j+2].type == MAPPATTERN_GRASS && (*info.theMap)[i+1][j+2].type == MAPPATTERN_GRASS ))
                    {
                        if(themap[i][j]!='*'&&themap[i+1][j]!='*'&&themap[i][j+1]!='*'&&themap[i+1][j+1]!='*'&&themap[i][j]!='D'&&themap[i+1][j]!='D'&&themap[i][j+1]!='D'&&themap[i+1][j+1]!='D')
                        {
                            // 找到符合条件的2*2空地，返回左下角坐标
                            x = i;
                            y = j;
                              cout<<x<<" "<<y<<endl;
                           return 1; // 返回1表示成功找到
                        }


                    }
                }
            }
        }
    }
    return 0; // 返回0表示未找到合适的空地
}
int find_kaoan(int beginx1,int beginy1,int& x, int& y)
{
    for (int i = beginx1; i < 128; i++) {
        for (int j = beginy1; j < 128; j++) {
            if (i + 1 < 128 && j + 1 < 128) {
                // 检查四个位置是否都是陆地
                if ((*info.theMap)[i][j].type == MAPPATTERN_OCEAN ){
                    // 检查高度是否相等
                    if((*info.theMap)[i+1][j].type == MAPPATTERN_GRASS
                            ||(*info.theMap)[i][j+1].type == MAPPATTERN_GRASS
                            ||(*info.theMap)[i][j-1].type == MAPPATTERN_GRASS
                            ||(*info.theMap)[i-1][j].type == MAPPATTERN_GRASS) {
                        x = i;
                        y = j;
                        return 1; // 返回1表示成功找到
                    }
                }
            }
        }
    }
    return 0; // 返回0表示未找到合适的空地
}

void UsrAI::processData()
{
    info=getInfo();
    std::vector<tagFarmer>Farmernumber;
    std::vector<tagFarmer>idleFarmer;               // 存储闲置的农民
    std::vector<tagFarmer>fishingFarmer;            // 存储捕鱼的农民
    std::vector<tagFarmer>cuttingFarmer;            // 存储砍树的农民
    std::vector<tagFarmer>stoneFarmer;
    std::vector<tagFarmer>buildingHomeFarmer;      // 存储建造房子的农民
    std::vector<tagFarmer>huntingFarmer;            // 存储抓羊的农民
    std::vector<tagFarmer>boatnumber;
    std::vector<tagBuilding>homenumber;             // 存储房子数量
    std::vector<tagBuilding>finishhomenumber;
    std::vector<tagBuilding>stocknumber;             // 存储仓库数量
    std::vector<tagBuilding>docknumber;             // 存储船坞数量
    std::vector<tagFarmer> sailingnumber;
    std::vector<tagArmy>shipnumber;
    std::vector<tagBuilding>granarynumber;
    std::vector<tagBuilding>farmnumber;
    std::vector<tagBuilding>finishfarmnumber;
    std::vector<tagBuilding>marketnumber;
    std::vector<tagBuilding>arrowtowernumber;
    std::vector<tagBuilding>armycampnumber;
    std::vector<tagBuilding>stablenumber;
    std::vector<tagBuilding>rangenumber;
    std::vector<tagResource>gazellenumber;
    std::vector<tagResource>elephantnumber;
    std::vector<pair<int,int>>builtpositions;
    std::map<int,tagResource> SN_to_Resource;
    std::map<int,int>SN_to_Sort;
    for(tagResource resource:info.resources) {
            SN_to_Resource[resource.SN] = resource;
            SN_to_Sort[resource.SN] = resource.Type;
        }
    for(tagResource resource:info.resources) {
            if(resource.Type==RESOURCE_GAZELLE)gazellenumber.push_back(resource);
            if(resource.Type==RESOURCE_ELEPHANT)elephantnumber.push_back(resource);
        }
    for(tagBuilding building:info.buildings) {
            if(building.Type == BUILDING_HOME) homenumber.push_back(building);
            if(building.Type == BUILDING_HOME&&building.Percent==100) finishhomenumber.push_back(building);
            if(building.Type == BUILDING_STOCK) stocknumber.push_back(building);
            if(building.Type == BUILDING_DOCK) docknumber.push_back(building);
            if(building.Type == BUILDING_GRANARY) granarynumber.push_back(building);
            if(building.Type == BUILDING_FARM) farmnumber.push_back(building);
            if(building.Type == BUILDING_FARM&&building.Percent==100) finishfarmnumber.push_back(building);
            if(building.Type==BUILDING_MARKET) marketnumber.push_back(building);
            if(building.Type==BUILDING_ARROWTOWER) arrowtowernumber.push_back(building);
            if(building.Type==BUILDING_ARMYCAMP) armycampnumber.push_back(building);
            if(building.Type==BUILDING_STABLE) stablenumber.push_back(building);
            if(building.Type==BUILDING_RANGE) rangenumber.push_back(building);
        }
    for(tagFarmer farmer:info.farmers) {
        if(farmer.FarmerSort == 0){
          if(farmer.NowState == HUMAN_STATE_IDLE) idleFarmer.push_back(farmer);  // 将闲置农民加入数组
          if(farmer.NowState == HUMAN_STATE_WORKING){
            int Type = SN_to_Sort[farmer.WorkObjectSN];
            // 状态对应
            if(Type == RESOURCE_TREE) cuttingFarmer.push_back(farmer);
            if(Type == BUILDING_HOME) buildingHomeFarmer.push_back(farmer);
            if(Type == RESOURCE_GAZELLE) huntingFarmer.push_back(farmer);
            if(Type == RESOURCE_ELEPHANT) huntingFarmer.push_back(farmer);
            if(Type==RESOURCE_STONE) stoneFarmer.push_back(farmer);
          }
        }
        if(farmer.FarmerSort==2 ) sailingnumber.push_back(farmer);
        if(farmer.FarmerSort==1 ) boatnumber.push_back(farmer);
    }
    for(tagFarmer farmer:info.farmers) {
        if(farmer.FarmerSort == 0) Farmernumber.push_back(farmer);
    }
    for(tagArmy army:info.armies){
        if( army.Sort==AT_SHIP)shipnumber.push_back(army);
    }

    for(int i=0;i<128;i++)
    {
        for(int j=0;j<128;j++)
        {
            themap[i][j]=0;
        }
    }
    for(auto& farmer :info.farmers)
    {
        themap[farmer.BlockDR][farmer.BlockUR]='*';
    }
    for(int i=0;i<128;i++)
    {
        for(int j=0;j<128;j++)
        {
            if((*info.theMap)[i][j].type == MAPPATTERN_UNKNOWN)
            {
                THEMAP[i][j]='?';
            }
        }
    }


if(stage==1)
    {
        // 在阶段1中，找到市镇中心后初始化房屋位置
        for (tagBuilding& building : info.buildings) //如果市镇中心空闲，且有足够的食物，且农民数量小于人口上限，则生产农民
        {
               if (building.Type == BUILDING_CENTER && building.Project == 0 && info.Meat >= BUILDING_CENTER_CREATEFARMER_FOOD &&
                       Farmernumber.size()<14) {
                   BuildingAction(building.SN, BUILDING_CENTER_CREATEFARMER);}
               if (building.Type == BUILDING_DOCK && building.Project==0 && info.Wood >=BUILDING_DOCK_CREATE_SAILING_WOOD && sailingnumber.size()<5)
                   BuildingAction(building.SN,BUILDING_DOCK_CREATE_SAILING);
                   break;

           }
        for (tagBuilding building : info.buildings) {
            if (building.Type == BUILDING_CENTER) {
                center_x = building.BlockDR+1; // 市镇中心中心坐标
                center_y = building.BlockUR+1;
            }
        }
        if(beginx==0 && beginy==0)
        {
            beginx=center_x+4;
            beginy=center_y-4;
        }
        if (homenumber.size() < 4) {
                    int targetx = 0, targety = 0;
                    // 尝试找到合适的空地
                    if (find_block(beginx, beginy, targetx, targety) == 1) {
                        // 检查是否已有足够的木材
                        if (info.Wood >= BUILD_HOUSE_WOOD && !idleFarmer.empty()) {
                            // 命令第一个闲置村民建造房屋
                            for (tagFarmer& farmer : idleFarmer) {
                                if (farmer.NowState == HUMAN_STATE_IDLE && farmer.FarmerSort == 0)
                                HumanBuild(farmer.SN, BUILDING_HOME, targetx, targety);
                                for (int i = 0; i < 2; i++) {
                                for (int j = 0; j < 2; j++) {
                                themap[targetx + i][targety + j] = 'H'; // 标记为房屋
                            }
                             }
                            }
                        }
                        beginx=targetx;
                        beginy=targety+2;
                    }
                }

        //进行各种资源的搜集
          if(finishhomenumber.size()>=4)
          {
             if(huntingFarmer.size()<4){
              int targetSN=-1;

              for (tagFarmer& farmer : info.farmers) {
                      if (farmer.NowState == HUMAN_STATE_IDLE && farmer.FarmerSort == 0) {
                          targetSN = findResource(info,RESOURCE_GAZELLE, farmer.DR, farmer.UR);
                          HumanAction(farmer.SN, targetSN);
                          }
              }

              }
             else{
                 if(docknumber.size()<=1)         //先找一个人去建船舱
                 {
                     int buildx=0;
                     int buildy=0;
                     if(find_block_dock(buildx,buildy)==1)
                    {
                            for (tagFarmer& farmer : idleFarmer)
                            HumanBuild(farmer.SN,BUILDING_DOCK,buildx,buildy);
                            for (int i = 0; i < 2; i++) {
                            for (int j = 0; j < 2; j++) {
                            themap[buildx + i][buildy + j] = 'D'; // 标记为房屋
                        }
                         }
                                     }

                 }
                 if(docknumber.size()==1)
                 {
                     int buildx=0;
                     int buildy=0;
                     if(find_block_dock2(buildx,buildy)==1)
                    {
                            for (tagFarmer& farmer : idleFarmer)
                            HumanBuild(farmer.SN,BUILDING_DOCK,buildx,buildy);
                                     }
                 }
                 int targetSN=-1;
                 //剩下的人去砍树
                 for (tagFarmer& farmer : info.farmers) {
                         if (farmer.NowState == HUMAN_STATE_IDLE && farmer.FarmerSort == 0 &&cuttingFarmer.size()<=4) {
                             targetSN = findResource(info,RESOURCE_TREE, farmer.DR, farmer.UR);
                             HumanAction(farmer.SN, targetSN);
                         }
                 }
                 int targetSN1=-1;
                 for (tagFarmer& farmer : info.farmers) {
                         if (farmer.NowState == HUMAN_STATE_IDLE && farmer.FarmerSort == 2 ) {
                             targetSN1 = findResource(info,RESOURCE_FISH, farmer.DR, farmer.UR);
                             HumanAction(farmer.SN, targetSN1);
                         }
                 }
                 if(Farmernumber.size()>13&&sailingnumber.size()>4)
                 {
                     for (tagBuilding& building : info.buildings){
                         if (building.Type == BUILDING_CENTER && building.Project == 0 && info.Meat >= BUILDING_CENTER_UPGRADE_FOOD)
                         {
                             BuildingAction(building.SN, BUILDING_CENTER_UPGRADE);
                         }
                     }
                     if(info.civilizationStage==CIVILIZATION_TOOLAGE)
                     {
                         stage=2;
                     }
                 }
                 }
          }
     }
if(stage==2)
        {
            int targetSN1=-1;
            for (tagFarmer& farmer : info.farmers) {
                    if (farmer.NowState == HUMAN_STATE_IDLE && farmer.FarmerSort == 2 ) {
                        targetSN1 = findResource(info,RESOURCE_FISH, farmer.DR, farmer.UR);
                        HumanAction(farmer.SN, targetSN1);
                    }
            }
            for (tagBuilding building : info.buildings) {
                if (building.Type == BUILDING_CENTER) {
                    center_x = building.BlockDR+1; // 市镇中心中心坐标
                    center_y = building.BlockUR+1;
                }
            }
            if(begin1x==0 && begin1y==0)
            {
                begin1x=center_x+6;
                begin1y=center_y-4;
            }
            if (homenumber.size()<8) {
                        int target1x = 0, target1y = 0;
                        // 尝试找到合适的空地
                        if (find_block(begin1x, begin1y, target1x, target1y) == 1) {
                            // 检查是否已有足够的木材
                            cout<<target1x<<' '<<target1y<<endl;
                            if (info.Wood >= BUILD_HOUSE_WOOD && !idleFarmer.empty()) {
                                // 命令第一个闲置村民建造房屋
                                for (tagFarmer& farmer : idleFarmer) {

                                    HumanBuild(farmer.SN, BUILDING_HOME, target1x, target1y);
                                    for (int i = 0; i < 2; i++) {
                                    for (int j = 0; j < 2; j++) {
                                    themap[target1x + i][target1y + j] = 'H';} // 标记为房屋
                                }
                                }
                                begin1x=target1x;
                                begin1y=target1y+2;
                            }

                        }

                    }

            if(homenumber.size()>=8){
                for (tagBuilding& building : info.buildings) //如果市镇中心空闲，且有足够的食物，且农民数量小于人口上限，则生产农民
                {
                       if (building.Type == BUILDING_CENTER && building.Project == 0 && info.Meat >= BUILDING_CENTER_CREATEFARMER_FOOD &&
                               Farmernumber.size()<26) {
                           BuildingAction(building.SN, BUILDING_CENTER_CREATEFARMER);}
                       if (building.Type == BUILDING_DOCK && building.Project==0 && info.Wood >=BUILDING_DOCK_CREATE_SAILING_WOOD && sailingnumber.size()<5)
                           BuildingAction(building.SN,BUILDING_DOCK_CREATE_SAILING);
                       if(building.Type==BUILDING_MARKET && building.Project == 0 && info.Meat >= BUILDING_MARKET_WOOD_UPGRADE_FOOD&&info.Wood >= BUILDING_MARKET_WOOD_UPGRADE_WOOD)
                           BuildingAction(building.SN,BUILDING_MARKET_WOOD_UPGRADE);
                           break;
                   }
                if(huntingFarmer.size()<6&&gazellenumber.size()>0){
                 int targetSN=-1;
                 for (tagFarmer& farmer : info.farmers) {
                         if (farmer.NowState == HUMAN_STATE_IDLE && farmer.FarmerSort == 0) {
                             targetSN = findResource(info,RESOURCE_GAZELLE, farmer.DR, farmer.UR);
                             HumanAction(farmer.SN, targetSN);
                             }
                 }
                 }
                if(huntingFarmer.size()<6&&gazellenumber.size()==0&&elephantnumber.size()>0){
                 int targetSN=-1;
                 for (tagFarmer& farmer : info.farmers) {
                         if (farmer.NowState == HUMAN_STATE_IDLE && farmer.FarmerSort == 0) {
                             targetSN = findResource(info,RESOURCE_ELEPHANT, farmer.DR, farmer.UR);
                             HumanAction(farmer.SN, targetSN);
                             }
                 }
                 }
                if(cuttingFarmer.size()<20)
                {
                    int targetSN=-1;
                    for (tagFarmer& farmer : info.farmers) {
                            if (farmer.NowState == HUMAN_STATE_IDLE && farmer.FarmerSort == 0) {
                                targetSN = findResource(info,RESOURCE_TREE, farmer.DR, farmer.UR);
                                HumanAction(farmer.SN, targetSN);
                            }
                    }
                }
                if(granarynumber.size()<1)
                {
                    if(begin2x==0 && begin2y==0)
                    {
                        begin2x=center_x-1;
                        begin2y=center_y-8;
                    }
                    int target2x = 0, target2y = 0;
                    // 尝试找到合适的空地
                    if (find_block_33(begin2x, begin2y, target2x, target2y) == 1) {
                        // 检查是否已有足够的木材
                        cout<<target2x<<' '<<target2y<<endl;
                        if (info.Wood >= BUILD_GRANARY_WOOD && !idleFarmer.empty()) {
                            // 命令第一个闲置村民建造房屋
                            for (tagFarmer& farmer : idleFarmer) {

                                HumanBuild(farmer.SN, BUILDING_GRANARY, target2x, target2y);
                                for (int i = 0; i < 3; i++) {
                                for (int j = 0; j < 3; j++) {
                                themap[target2x + i][target2y + j] = 'H';} // 标记为已建
                            }
                            }
                            begin2x=target2x;
                            begin2y=target2y;
                        }

                    }
                }
                for (tagBuilding building : info.buildings) {
                    if (building.Type == BUILDING_GRANARY && building.Percent == 100) {
                        if(granarynumber.size()==1&&marketnumber.size()<1)
                        {
                            if(begin4x==0 && begin4y==0)
                            {
                                begin4x=center_x-1;
                                begin4y=center_y+4;
                            }
                            int target4x = 0, target4y = 0;
                            // 尝试找到合适的空地
                            if (find_block_33(begin4x, begin4y, target4x, target4y) == 1) {
                                // 检查是否已有足够的木材
                                cout<<target4x<<' '<<target4y<<endl;
                                if (info.Wood >= BUILD_MARKET_WOOD) {
                                    // 命令第一个闲置村民建造房屋
                                    for (tagFarmer& farmer : idleFarmer) {
                                        HumanBuild(farmer.SN, BUILDING_MARKET, target4x, target4y);
                                        for (int i = 0; i < 3; i++) {
                                        for (int j = 0; j < 3; j++) {
                                        themap[target4x + i][target4y + j] = 'H';} // 标记为已建
                                    }
                                    }
                                }
                            }
                        }
                    }
                }
                for (tagBuilding building : info.buildings) {
                    if (building.Type == BUILDING_MARKET && building.Percent == 100) {
                        if(granarynumber.size()==1&&marketnumber.size()==1&&farmnumber.size()<4)
                        {
                            if(begin3x==0 && begin3y==0)
                            {
                                begin3x=center_x-4;
                                begin3y=center_y-8;
                            }
                            int target3x = 0, target3y = 0;
                            // 尝试找到合适的空地
                            if (find_block_33farm(begin3x, begin3y, target3x, target3y) == 1) {
                                // 检查是否已有足够的木材
                                cout<<target3x<<' '<<target3y<<endl;
                                if (info.Wood >= BUILD_FARM_WOOD && !idleFarmer.empty()) {
                                    // 命令第一个闲置村民建造房屋
                                    for (tagFarmer& farmer : idleFarmer) {

                                        HumanBuild(farmer.SN, BUILDING_FARM, target3x, target3y);
                                        for (int i = 0; i < 3; i++) {
                                        for (int j = 0; j < 3; j++) {
                                        themap[target3x + i][target3y + j] = 'H';} // 标记为已建
                                    }
                                    }
                                    begin3x=target3x;
                                    begin3y=target3y;
                                }
                            }
                        }
                    }
                }
                if(finishfarmnumber.size()==4)
                {
                    stage=3;
                }
        }
     }
if(stage==3)
{
    int targetSN2=-1;
    for (tagFarmer& farmer : info.farmers) {
            if (farmer.NowState == HUMAN_STATE_IDLE && farmer.FarmerSort == 2 ) {
                targetSN2 = findResource(info,RESOURCE_FISH, farmer.DR, farmer.UR);
                HumanAction(farmer.SN, targetSN2);
            }
    }
    for (tagBuilding building : info.buildings) {
        if (building.Type == BUILDING_CENTER) {
            center_x = building.BlockDR+1; // 市镇中心中心坐标
            center_y = building.BlockUR+1;
        }
    }
    armycenterx=128-center_x;
    armycentery=128-center_y;
    if(begin5x==0 && begin5y==0)
    {
                    begin5x=center_x+8;
                    begin5y=center_y-8;
    }
    if (homenumber.size()<13) {
                            int target5x = 0, target5y = 0;
                            // 尝试找到合适的空地
                            if (find_block(begin5x, begin5y, target5x, target5y) == 1) {
                                cout<<target5x<<' '<<target5y<<endl;
                                if (info.Wood >= BUILD_HOUSE_WOOD&&!idleFarmer.empty()) {
                                    // 命令第一个闲置村民建造房屋
                                    for (tagFarmer& farmer : idleFarmer) {
                                        HumanBuild(farmer.SN, BUILDING_HOME, target5x, target5y);
                                        for (int i = 0; i < 2; i++) {
                                        for (int j = 0; j < 2; j++) {
                                        themap[target5x + i][target5y + j] = 'H';} // 标记为房屋
                                    }
                                    }
                                    begin5x=target5x;
                                    begin5y=target5y+2;
                                }
                            }
                        }
    if(finishhomenumber.size()>=12){
    for (tagBuilding& building : info.buildings) //如果市镇中心空闲，且有足够的食物，且农民数量小于人口上限，则生产农民
    {
           if (building.Type == BUILDING_CENTER && building.Project == 0 && info.Meat >= BUILDING_CENTER_CREATEFARMER_FOOD && Farmernumber.size()<30) {
               BuildingAction(building.SN, BUILDING_CENTER_CREATEFARMER);}
           if (building.Type == BUILDING_DOCK && building.Project==0 && info.Wood >=BUILDING_DOCK_CREATE_SAILING_WOOD && sailingnumber.size()<5)
               BuildingAction(building.SN,BUILDING_DOCK_CREATE_SAILING);
           if (building.Type == BUILDING_DOCK && building.Project==0 && info.Wood >=BUILDING_DOCK_CREATE_SHIP_WOOD && shipnumber.size()<12)
               BuildingAction(building.SN,BUILDING_DOCK_CREATE_SHIP);
           if(building.Type==BUILDING_MARKET && building.Project == 0 && info.Meat >= BUILDING_MARKET_FARM_UPGRADE_FOOD&&info.Wood >= BUILDING_MARKET_FARM_UPGRADE_WOOD)
               BuildingAction(building.SN,BUILDING_MARKET_FARM_UPGRADE);
           if(building.Type==BUILDING_MARKET && building.Project == 0 && info.Meat >= BUILDING_MARKET_WOOD_UPGRADE_FOOD&&info.Wood >= BUILDING_MARKET_WOOD_UPGRADE_WOOD)
               BuildingAction(building.SN,BUILDING_MARKET_WOOD_UPGRADE);
           if (building.Type == BUILDING_DOCK && building.Project==0 && info.Wood >=BUILDING_DOCK_CREATE_WOOD_BOAT_WOOD && shipnumber.size()>=12&&boatnumber.size()<1&&buildboat==0){
               BuildingAction(building.SN,BUILDING_DOCK_CREATE_WOOD_BOAT);
               buildboat=1;}
               break;
       }
    if(finishfarmnumber.size()==0)
    {
        if(farmnumber.size()<4)
        {
            if(begin7x==0 && begin7y==0)
            {
                begin7x=center_x-4;
                begin7y=center_y-8;
            }
            int target7x = 0, target7y = 0;
            // 尝试找到合适的空地
            if (find_block_33farm(begin7x, begin7y, target7x, target7y) == 1) {
                // 检查是否已有足够的木材
                cout<<target7x<<' '<<target7y<<endl;
                if (info.Wood >= BUILD_FARM_WOOD && !idleFarmer.empty()) {
                    // 命令第一个闲置村民建造房屋
                    for (tagFarmer& farmer : idleFarmer) {

                        HumanBuild(farmer.SN, BUILDING_FARM, target7x, target7y);
                        for (int i = 0; i < 3; i++) {
                        for (int j = 0; j < 3; j++) {
                        themap[target7x + i][target7y + j] = 'H';} // 标记为已建
                    }
                    }
                    begin7x=target7x;
                    begin7y=target7y;
                }
            }
        }
    }
    if(gazellenumber.size()>0||elephantnumber.size()>0)
    {
        if(huntingFarmer.size()<6&&gazellenumber.size()>0){
         int targetSN=-1;
         for (tagFarmer& farmer : info.farmers) {
                 if (farmer.NowState == HUMAN_STATE_IDLE && farmer.FarmerSort == 0) {
                     targetSN = findResource(info,RESOURCE_GAZELLE, farmer.DR, farmer.UR);
                     HumanAction(farmer.SN, targetSN);
                     }
         }
         }
        if(huntingFarmer.size()<6&&gazellenumber.size()==0&&elephantnumber.size()>0){
            cout<<1<<endl;
            cout<<elephantnumber.size()<<endl;
         int targetSNele=-1;
         for (tagFarmer& farmer : info.farmers) {
                 if (farmer.NowState == HUMAN_STATE_IDLE && farmer.FarmerSort == 0) {
                     targetSNele = findResource_yiqi(info,RESOURCE_ELEPHANT, farmer.DR, farmer.UR);
                     HumanAction(farmer.SN, targetSNele);
                     }
         }
         }
        if(cuttingFarmer.size()<25)
        {
            int targetSN=-1;
            for (tagFarmer& farmer : info.farmers) {
                    if (farmer.NowState == HUMAN_STATE_IDLE && farmer.FarmerSort == 0) {
                        targetSN = findResource(info,RESOURCE_TREE, farmer.DR, farmer.UR);
                        HumanAction(farmer.SN, targetSN);
                    }
            }
        }
    }
    else
    {

        if(cuttingFarmer.size()<28)
        {
            int targetSN=-1;
            for (tagFarmer& farmer : info.farmers) {
                    if (farmer.NowState == HUMAN_STATE_IDLE && farmer.FarmerSort == 0) {
                        targetSN = findResource(info,RESOURCE_TREE, farmer.DR, farmer.UR);
                        HumanAction(farmer.SN, targetSN);
                    }
            }
        }
        if(stoneFarmer.size()<2)
        {
            int targetSN=-1;
            for (tagFarmer& farmer : info.farmers) {
                    if (farmer.NowState == HUMAN_STATE_IDLE && farmer.FarmerSort == 0) {
                        targetSN = findResource(info,RESOURCE_STONE, farmer.DR, farmer.UR);
                        HumanAction(farmer.SN, targetSN);
                    }
            }
        }
    }

    if(tingkaox==0&&tingkaoy==0)
    {
        for (tagBuilding building : info.buildings) {
            if (building.Type == BUILDING_DOCK) {
                tingkaox = building.BlockDR; // 市镇中心中心坐标
                tingkaoy = building.BlockUR+8;
            }
        }
    }
    tagArmy* enemy_army = nullptr;
    for (tagArmy& army : info.enemy_armies) {
        // 这里假设目标定为敌方战船
        if (army.Sort == AT_SHIP) {
            enemy_army = &army;
    DebugText("找到敌军战船");
            break;
        }
    }
    // 如果有敌军战船，再找我方战船
    if (enemy_army != nullptr) {
        for (tagArmy& army : info.armies) {
            // 命令所有空闲的战船攻击敌军战船
            if (army.Sort == AT_SHIP && (army.NowState == HUMAN_STATE_IDLE||army.NowState==HUMAN_STATE_WALKING)) {
                HumanAction(army.SN, enemy_army->SN);
                break;
            }
        }
    }

    if(shipnumber.size()<12&&chufa==0)
    {
        for(tagArmy &army:info.armies){
            if(army.Sort=AT_SHIP)
            {
                HumanMove(army.SN,tingkaox*BLOCKSIDELENGTH,tingkaoy*BLOCKSIDELENGTH);
                break;
            }
        }
    }
        if((shipnumber.size()>=12||chufa==1)&&enemy_army==nullptr&&zhanchuanhuijia==0)
        {
            chufa=1;
            for(tagArmy &army:info.armies){
                if(army.Sort=AT_SHIP)
                {
                    HumanMove(army.SN,armycenterx*BLOCKSIDELENGTH,armycentery*BLOCKSIDELENGTH);
                    break;
                }
            }
        }
        for(int i=0;i<128;i++)
        {
            for(int j=0;j<128;j++)
            {
                if(i-1>=0&&i+1<128&&j-1>=0&&j+1<128)
                {
                    if(((*info.theMap)[i][j].type==MAPPATTERN_GRASS&&(*info.theMap)[i-1][j].type==MAPPATTERN_OCEAN)
                            ||((*info.theMap)[i][j].type==MAPPATTERN_GRASS&&(*info.theMap)[i+1][j].type==MAPPATTERN_OCEAN)
                            ||((*info.theMap)[i][j].type==MAPPATTERN_GRASS&&(*info.theMap)[i][j+1].type==MAPPATTERN_OCEAN)
                            ||((*info.theMap)[i][j].type==MAPPATTERN_GRASS&&(*info.theMap)[i][j-1].type==MAPPATTERN_OCEAN))
                    {
                        if(THEMAP[i][j]=='?')
                        {
                            denglux=i;
                            dengluy=j;

                        }
                    }
                }

            }
        }

        for (tagFarmer& farmer : info.farmers) {
                if (farmer.FarmerSort == 1 && farmer.NowState == HUMAN_STATE_IDLE && farmer.Resource < 5)
                {
                    int targetx=0;
                    int targety=0;
                    if(find_kaoan(tingkaox,tingkaoy,targetx,targety)==1)
                    HumanMove(farmer.SN,(targetx+0.5)*BLOCKSIDELENGTH,(targety+0.5)*BLOCKSIDELENGTH);
                    break;
                }
        }
        tagFarmer* idleShip = nullptr;
            for (tagFarmer& farmer : info.farmers) {
                // 单位类型为运输船，状态为空闲，未满载
                if (farmer.FarmerSort == 1 && farmer.NowState == HUMAN_STATE_IDLE && farmer.Resource < 5) {
                    idleShip = &farmer;
                    yidongduian=0;
                    break;
                }
            }
            // 如果有空闲运输船，再找空闲农民
            // 假设运输船可达
            if (idleShip != nullptr) {
                for (tagFarmer& farmer : info.farmers) {
                    // 命令所有空闲的农民上船
                    if (farmer.FarmerSort == 0 && farmer.NowState == HUMAN_STATE_IDLE) {
                        HumanAction(farmer.SN, idleShip->SN);
                    }
                }
            }
            for (tagFarmer& farmer : info.farmers){
                if(farmer.FarmerSort == 1 && farmer.Resource==5)
                {
                    if(denglux!=0&&dengluy!=0&&yidongduian==0){
                    HumanMove(farmer.SN,(denglux+0.5)*BLOCKSIDELENGTH,(dengluy+0.5)*BLOCKSIDELENGTH);
                    yidongduian=1;
                    zhanchuanhuijia=1;
                    }
                    break;
                }
            }
            if(enemy_army==nullptr&&zhanchuanhuijia==1)
            {
                for(tagArmy &army:info.armies){
                    if(army.Sort=AT_SHIP)
                    {
                        HumanMove(army.SN,tingkaox*BLOCKSIDELENGTH,tingkaoy*BLOCKSIDELENGTH);
                        break;
                    }
                }
            }
}
}
 }

